package com.community.process._1010.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 死信队列配置
 */
@Configuration
@Slf4j
public class ProcessOrderDelayedQueueConfig {
    public static final String QUEUE_NAME = "process.order.delayed.queue";
    public static final String EXCHANGE_NAME = "process.order.delayed.exchange";
    public static final String ROUTING_KEY = "process.order.delayed.routingKey";

    @Bean
    public Queue processOrderDelayedQueue() {
        return new Queue(QUEUE_NAME,true);
    }

    /**
     * 基于插件 延时队列
     * @return
     */
    @Bean
    public Exchange processOrderDelayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        //设置交换机支持延迟消息推送
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(EXCHANGE_NAME,"x-delayed-message",true,false,arguments);
    }

    /**
     * 队列绑定交换机
     * @return
     */
    @Bean
    public Binding processOrderDelayedQueueBindingProcessOrderDelayedExchange(){
        return BindingBuilder.bind(processOrderDelayedQueue()).to(processOrderDelayedExchange()).with(ROUTING_KEY).noargs();
    }

}
